<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <form action="register">
        <div class="input-prepend restyle">
          <input
            v-model="user.mobile"
            type="text"
            placeholder="手机号">
          <i class="iconfont icon-phone"/>
        </div>
        <div class="input-prepend">
          <input
            v-model="user.password"
            type="password"
            placeholder="密码">
          <i class="iconfont icon-password"/>
        </div>
        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="登录"
            @click="submitLogin()">
        </div>
        <div class="btn">
          <input
            type="button"
            class="sign-in-button"
            value="重置密码"
            @click="forgetPassword()">
        </div>
      </form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li><a id="weixin" class="weixin" href="http://localhost:8150/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a></li>
          <li><a id="qq" class="qq" target="_blank" href="javascript:void(0)" @click="loginQQ()"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" title="重置密码">
      <el-form :model="user">
        <el-form-item :label-width="formLabelWidth" label="输入注册手机号">
          <el-input v-model="user.mobile" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="输入新密码">
          <el-input v-model="user.password" type="password" autocomplete="off"/>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="输入短信验证码">
          <el-input v-model="user.code" autocomplete="off"/>
          <a
            href="javascript:"
            type="button"
            style="position:absolute;right: 10px;"
            @click="getCodeFun()">{{ codeText }}</a>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import '~/assets/css/sign.css'
import '~/assets/css/iconfont.css'
import cookie from 'js-cookie'
import loginApi from '~/api/login'
import userApi from '~/api/userCenter'
import registerApi from '~/api/register'
export default {
  layout: 'sign',

  data() {
    return {
      user: {
        mobile: '',
        password: ''
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: '120px',
      sending: false, // 是否发送验证码
      second: 60, // 倒计时间
      codeText: '获取验证码'
    }
  },

  methods: {
    // 登录
    submitLogin() {
      loginApi.login(this.user).then(res => {
        cookie.set('cjc_edu_token', res.data.token, { domain: 'localhost' })
        // 跳转到首页
        window.location.href = '/'
      })
    },
    // 获取验证码
    getCodeFun() {
      if (this.sending) {
        return
      }
      this.sending = true
      registerApi.sendSMS(this.user.mobile).then(res => {
        this.timeDown()
        this.$message({
          type: 'success',
          message: '短信发送成功'
        })
      })
    },

    // 倒计时
    timeDown() {
      const result = setInterval(() => {
        this.codeText = this.second
        this.second--
        if (this.second < 0) {
          clearInterval(result)
          this.sending = false
          this.second = 60
          this.codeText = '获取验证码'
        }
      }, 1000)
    },
    forgetPassword() {
      this.dialogFormVisible = true
    },
    resetPassword() {
      userApi.resetPassword(this.user).then(res => {
        if (res.success) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.dialogFormVisible = false
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    }
  }
}
</script>
